package cn.bdqn.config;

import com.sun.org.apache.regexp.internal.RE;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * RabbitMQ的配置类
 */
@Configuration
public class RabbitMQConfig {

    @Value("${mq.kill.exchange}")
    private String topExchangeName;
    @Value("${mq.kill.success.email.queue}")
    private String emailQueueName;
    @Value("${mq.kill.success.email.router.key}")
    private String emailRouterKey;

    @Value("${mq.kill.dead.exchange}")
    private String deadExchangeName;
    @Value("${mq.kill.success.dead.queue}")
    private String deadQueueName;
    @Value("${mq.kill.success.dead.router.key}")
    private String deadRouterKey;
    @Value("${mq.kill.success.dead.ttl}")
    private Integer ttl;

    @Value("${mq.kill.dead.base.exchange}")
    private String deadBaseExchangeName;
    @Value("${mq.kill.success.dead.base.queue}")
    private String deadBaseQueueName;
    @Value("${mq.kill.success.dead.base.router.key}")
    private String deadBaseRouterKey;


    //邮件队列
    @Bean
    public Queue emailQueue(){
        return new Queue(emailQueueName,true);
    }
    //邮件交换机
    @Bean
    public TopicExchange topicExchange(){
        return new TopicExchange(topExchangeName,true,true);
    }
    //邮件交换机+邮件路由 --》 邮件队列
    @Bean
    public Binding binding(){
        return BindingBuilder.bind(emailQueue()).to(topicExchange()).with(emailRouterKey);
    }

    //订单超时未支付的死信队列
    @Bean
    public Queue deadQueue(){
        Map<String,Object> map = new HashMap<String,Object>();
        //死信交换机
        map.put("x-dead-letter-exchange",deadExchangeName);
        //死信路由
        map.put("x-dead-letter-routing-key",deadRouterKey);
        //过期时间
        map.put("x-message-ttl",ttl);
        return new Queue(deadQueueName,true,true,true,map);
    }
    //基本交换机
    @Bean
    public TopicExchange deadBaseTopicExchange(){
        return new TopicExchange(deadBaseExchangeName,true,true);
    }
    //基本交换机+基本路由 --》 死信队列
    @Bean
    public Binding baseDeadBinding(){
        return BindingBuilder.bind(deadQueue()).to(deadBaseTopicExchange()).with(deadBaseRouterKey);
    }

    //死信交换机
    @Bean
    public TopicExchange deadTopicExchange(){
        return new TopicExchange(deadExchangeName,true,true);
    }
    //真正队列
    @Bean
    public Queue deadBaseQueue(){
        return new Queue(deadBaseQueueName,true);
    }
    //死信交换机+死信路由 --》 真正队列
    @Bean
    public Binding deadBinding(){
        return BindingBuilder.bind(deadBaseQueue()).to(deadTopicExchange()).with(deadRouterKey);
    }

}
